<?php
/**
 * Created by PhpStorm.
 * User: 朱杰
 * Date: 2017/2/25
 * Time: 13:58
 */

namespace app\controllers;

use app\models\Find;
use app\models\Get;
use app\models\UploadForm;
use app\models\Users;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\UploadedFile;
use yii\db\Query;
class PublishController extends Controller
{
    /**
     * @inheritdoc
     */

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function beforeAction($action)
    {
        $session=Yii::$app->session;
        if(!isset($session["username"])){
            return $this->redirect("?r=site/signin");
        }
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    /**
     * Displays homepage.
     *
     * @return string
     * 如果第一次进入，返回1
     * 验证如果漏输，返回2
     * 验证如果正确，返回3
     *
     * 失物类别、时间、地点、所属学院、联系人。电话。qq、usersid 都必须要填好
     */
    public function actionFind_publish()
    {
        $request=Yii::$app->request;
        $session=Yii::$app->session;
        $model = new UploadForm();
        $username=$session["username"];
        $user=Users::find()->where(["username"=>$username])->one();
        $userid=$user["id"];


        //获得失物类别和学院

        $types=(new Query())
        	->select(["name"])
        	->from("types")
        	->orderBy("id")
        	->all();
        $ai=(new Query())
        	->select(["name"])
        	->from("ai")
        	->orderBy("id")
        	->all();


        if($request->isPost){

            // get  寻物启事，寻找失物的主人
            $find=new Get();
            $find->class=$request->post("ttype");
            $find->xueyuan=$request->post("tbelong");
            $find->time=$request->post("tdate");
            $find->tel=$request->post("ttel");
            $find->qq=$request->post("tqq");
            $find->more=$request->post("tmes");
            $find->users_id=$userid;   //发布者的id
            $find->person=$request->post("towner");
            $find->place=$request->post("tplace");
            $find->able=intval($request->post("tperoid"));
            $find->xuanshang=$request->post("treturn");
            $find->name=$request->post("tname");
            $find->publish_time=time();
            $model->file=UploadedFile::getInstance($model,"file");

//            如果上传了图片，就先保存其他信息，如果其他信息能够保存，将图片存入数据库
            if ($model->file&&$model->validate()){
                $md=md5(time());
                $url="uploads/".$md.".".$model->file->extension;
                $find->img_url=$url;
                if ($find->save()){
                    $model->file->saveAs($url);
                    return $this->render("find_publish",["model"=>$model,"mes"=>3,"types"=>$types,"ai"=>$ai]);
                }else{
                    return $this->render("find_publish",["model"=>$model,"mes"=>2,"types"=>$types,"ai"=>$ai]);
                }
            }else{
                if ($find->save()){
                    return $this->render("find_publish",["model"=>$model,"mes"=>3,"types"=>$types,"ai"=>$ai]);
                }else{
                    return $this->render("find_publish",["model"=>$model,"mes"=>2,"types"=>$types,"ai"=>$ai]);
                }
            }
        }
        return $this->render('find_publish',["model"=>$model,"mes"=>1,"types"=>$types,"ai"=>$ai]);
    }

    public function actionLost_publish()
    {
        $request=Yii::$app->request;
        $session=Yii::$app->session;
        $model = new UploadForm();

        $username=$session["username"];
        $user=Users::find()->where(["username"=>$username])->one();
        $userid=$user["id"];

        //获得失物类别和学院
        $types=(new Query())
        	->select(["name"])
        	->from("types")
        	->orderBy("id")
        	->all();
        $ai=(new Query())
        	->select(["name"])
        	->from("types")
        	->orderBy("id")
        	->all();


        
        if($request->isPost){
            // lost 寻物启事，丢失物品的信息
            $find=new Find();
            $find->class=$request->post("ttype");
            $find->xueyuan=$request->post("tbelong");
            $find->time=$request->post("tdate");
            $find->tel=$request->post("ttel");
            $find->qq=$request->post("tqq");
            $find->more=$request->post("tmes");
            $find->users_id=$userid;   //失物者的id
            $find->person=$request->post("towner");
            $find->place=$request->post("tplace");
            $find->able=intval($request->post("tperoid"));
            $find->xuanshang=$request->post("treturn");
            $find->name=$request->post("tname");
            $find->publish_time=time();
            $model->file=UploadedFile::getInstance($model,"file");

//            如果上传了图片，就先保存其他信息，如果其他信息能够保存，将图片存入数据库
            if ($model->file&&$model->validate()){
                $md=md5(time());
                $url="uploads/".$md.".".$model->file->extension;
                $find->img_url=$url;
                if ($find->save()){
                    $model->file->saveAs($url);
                    return $this->render("lost_publish",["model"=>$model,"mes"=>3,"types"=>$types,"ai"=>$ai]);
                }else{
                    return $this->render("lost_publish",["model"=>$model,"mes"=>2,"types"=>$types,"ai"=>$ai]);
                }
            }else{
                if ($find->save()){
                    return $this->render("lost_publish",["model"=>$model,"mes"=>3,"types"=>$types,"ai"=>$ai]);
                }else{
                    return $this->render("lost_publish",["model"=>$model,"mes"=>2,"types"=>$types,"ai"=>$ai]);
                }
            }
        }
        return $this->render('lost_publish',["model"=>$model,"mes"=>1,"types"=>$types,"ai"=>$ai]);
    }

    /**
     * Login action.
     *
     * @return string
     */


    /**
     * Displays contact page.
     *
     * @return string
     */


    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }
}
